package com.wu.controller;/**
 * @author sun
 * @date 2022/3/21 - 7:45
 */

import com.wu.pojo.Historysearch;
import com.wu.pojo.Images;
import com.wu.pojo.vo.HistorysearchVo;
import com.wu.service.HistorysearchService;
import com.wu.service.ImagesService;
import com.wu.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description: TODO
 * @author 孙
 * @date 2022/3/21 7:45
 * @version 1.0
 */
@RestController
@Api(tags="Historysearch模块")
@CrossOrigin
@RequestMapping("historysearch")
public class HistorysearchController {
    @Autowired
    private HistorysearchService historysearchService;
    @Autowired
    private ImagesService imagesService;
    @PostMapping("/insertHistorysearch")
    @ApiOperation(value="增加Historysearch")
    public R insertHistorysearch(@RequestBody Historysearch historysearch){
        try {
            historysearchService.insertSelective(historysearch);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }

    @PostMapping("/insertHistorysearchVo")
    @ApiOperation(value="增加HistorysearchVo")
    public R insertHistorysearch(@RequestBody HistorysearchVo historysearchVo){
        try {
            historysearchService.insertSelective(historysearchVo);
             List<Images> images = historysearchVo.getImages();
            if (images!=null && images.size()>0){
                for (Images image : images) {
                    imagesService.insertSelective(image);
                }
            }
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }

    }


    @GetMapping("/deleteHistorysearch/{id}")
    @ApiOperation(value="删除Historysearch")
    public R deleteHistorysearch(Integer id){
        try {
            historysearchService.deleteByPrimaryKey(id);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PutMapping("/updateHistorysearch")
    @ApiOperation(value="更新Historysearch")
    public R updateHistorysearch(@RequestBody Historysearch historysearch){
        try {
            historysearchService.updateByPrimaryKeySelective(historysearch);
            return R.ok();
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findHistorysearch")
    @ApiOperation(value="查询HistorysearchList")
    public R findHistorysearch(Historysearch historysearch){
        try {
            List<HistorysearchVo> list= historysearchService.findHistorysearch(historysearch);
            return R.ok().data("historysearchList",list);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @GetMapping("/findHistorysearchById/{id}")
    @ApiOperation(value="通过id查询Historysearch")
    public R findHistorysearch(@PathVariable("id") Integer id){
        try {
            Historysearch historysearch= historysearchService.selectByPrimaryKey(id);
            return R.ok().data("historysearch",historysearch);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }
}
